package com.jjq.articlecomment.service.impl;

import com.jjq.articlecomment.mapper.CommentRepository;
import com.jjq.articlecomment.entity.Comment;
import com.jjq.articlecomment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentRepository commentRepository;

    public List<Comment> findall(){
        return commentRepository.findAll();
    }

    @Override
    public void add(Comment comment) {
        commentRepository.save(comment);
    }

    @Override
    public void update(Comment comment) {
        commentRepository.save(comment);
    }

    @Override
    public void delete(String id) {
        commentRepository.deleteById(id);
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void Thumbup(String id) {
        //查询对象
        Query query=Query.query(Criteria.where("_id").is(id));
        //更新条件
        Update update = new Update();
        update.inc("likenum");

        //执行更新
        mongoTemplate.updateFirst(query,update,"comment");
    }

}
